package cn.csc.module.shop.controller.app.detail;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.security.core.annotations.PreAuthenticated;
import cn.csc.module.shop.api.detail.dto.ShopDetailRelateReqDTO;
import cn.csc.module.shop.controller.admin.audit.vo.ShopAuditRespVO;
import cn.csc.module.shop.controller.admin.detail.vo.ShopDetailRespVO;
import cn.csc.module.shop.controller.admin.detail.vo.ShopDetailUpdateReqVO;
import cn.csc.module.shop.controller.app.detail.vo.AppShopDetailByShopIdsRespVO;
import cn.csc.module.shop.controller.app.detail.vo.AppShopDetailCreateReqVO;
import cn.csc.module.shop.controller.app.detail.vo.AppShopDetailSimpleRespVO;
import cn.csc.module.shop.controller.app.detail.vo.AppShopDetailUpdateShopLogoReqVO;
import cn.csc.module.shop.convert.audit.ShopAuditConvert;
import cn.csc.module.shop.convert.detail.ShopDetailConvert;
import cn.csc.module.shop.dal.dataobject.audit.ShopAuditDO;
import cn.csc.module.shop.dal.dataobject.detail.ShopDetailDO;
import cn.csc.module.shop.service.audit.ShopAuditService;
import cn.csc.module.shop.service.detail.ShopDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * app-api 店铺详情 controller
 * @author liuchuang
 * @version 1.0
 * @date 2024/1/12 17:59
 * @modify
 */
@Api(tags = "app后台 - 店铺详情")
@RestController
@RequestMapping("/shop/detail")
@Validated
public class AppShopDetailController {

    @Resource
    private ShopDetailService shopDetailService;

    @Resource
    private ShopAuditService shopAuditService;


    @PostMapping("/create")
    @ApiOperation("创建店铺")
    @PreAuthenticated
    public CommonResult<BigInteger> createShop(@Valid @RequestBody AppShopDetailCreateReqVO reqVO){
        return success(shopDetailService.createDetailAndShopUser(reqVO));
    }

    @PostMapping("/idCardNoValid")
    @ApiOperation("证件号唯一性校验")
    @PreAuthenticated
    public CommonResult<Boolean> idCardNoValid(@RequestParam String idCardNo){
        return success(shopDetailService.idCardNoValid(idCardNo));
    }

    @PostMapping("/relate")
    @ApiOperation("从华商贸关联店铺")
    @PreAuthenticated
    public CommonResult<Boolean> relateShopDetail(@Valid @RequestBody ShopDetailRelateReqDTO reqDTO) {
        return success(shopDetailService.relateShopDetail(reqDTO));
    }

    @GetMapping("/get")
    @ApiOperation("获得店铺详情")
    @PreAuthenticated
    public CommonResult<ShopDetailRespVO> getDetail(@RequestParam("id") BigInteger id) {
        ShopDetailDO detail = shopDetailService.getDetail(id);
        return success(ShopDetailConvert.INSTANCE.convert(detail));
    }

    @GetMapping("/getSimpleDetail")
    @ApiOperation("获得简单店铺详情")
    public CommonResult<AppShopDetailSimpleRespVO> getSimpleDetail(@RequestParam("id") BigInteger id) {
        ShopDetailDO detail = shopDetailService.getSimpleDetail(id);
        return success(ShopDetailConvert.INSTANCE.convert3(detail));
    }

    @GetMapping("/getByShopIds")
    @ApiOperation("通过店铺ID列表获得店铺详情")
    public CommonResult<List<AppShopDetailByShopIdsRespVO>> getByShopIds(@RequestParam("shopIds") String shopIds) {
        List<BigInteger> ids = Arrays.stream(shopIds.split(",")).map(BigInteger::new).collect(Collectors.toList());
        List<ShopDetailDO> list = shopDetailService.getDetailList(ids);
        List<ShopDetailDO> collect = list.stream().filter(s -> StringUtils.hasLength(s.getAppUserId())).collect(Collectors.toList());
        return success(ShopDetailConvert.INSTANCE.convertList2(collect));
    }

    @GetMapping("/getCurrentUser")
    @ApiOperation("获得当前用户的店铺详情")
    @PreAuthenticated
    public CommonResult<ShopDetailRespVO> getCurrentUser() {
        ShopDetailDO detail = shopDetailService.getDetailByUserId(BigInteger.valueOf(getLoginUserId()));
        return success(ShopDetailConvert.INSTANCE.convert(detail));
    }

    @PutMapping("/update")
    @ApiOperation("更新店铺详情")
    @PreAuthenticated
    public CommonResult<Boolean> updateDetail(@Valid @RequestBody ShopDetailUpdateReqVO updateReqVO) {
        shopDetailService.updateDetail(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateShopLogo")
    @ApiOperation("更新ShopLogo")
    @PreAuthenticated
    public CommonResult<Boolean> updateShopLogo(@Valid @RequestBody AppShopDetailUpdateShopLogoReqVO updateReqVO) {
        shopDetailService.updateShopLogo(updateReqVO);
        return success(true);
    }

    @GetMapping("/getLatestAudit")
    @ApiOperation("获取最新的审核信息")
    @PreAuthenticated
    public CommonResult<ShopAuditRespVO> getLatestAudit(@RequestParam("shopId") BigInteger shopId) {
        ShopAuditDO latestByShopId = shopAuditService.getLatestByShopId(shopId);
        return success(ShopAuditConvert.INSTANCE.convert(latestByShopId));
    }



}
